import appRouterManger from './routerInit'
import { transformRouterHandle } from './routerHandleTransform'
import { useHallRouterStore } from '@/store/modules/hallRouter'
import { useUserStore } from '@/store/modules/user'


const loginPath = 'Login'
const notNeedLoginPaths = [loginPath]
if (appRouterManger.router.getRoutes().length === 0) {
    const routers = [
        {
            path: '/',
            redirect: '/login'
        },
        {
            path: '/login',
            name: loginPath,
            component: 'views/login/LoginView.vue',
        },
        {
            path: '/app',
            name: 'app',
            children: [
                {
                    path: 'CampUpload',
                    name: 'CampUpload',
                    component: 'views/app/CampUpload.vue',
                },
                {
                    path: 'PostUpload',
                    name: 'PostUpload',
                    component: 'views/app/PostUpload.vue',
                },
            ]
        },
    ]
    transformRouterHandle(routers)
    routers.forEach(item => {
        appRouterManger.router.addRoute(item)
    })
}

appRouterManger.router.beforeEach(async(to, from) => {
    console.log('router beforeEach: to', to, to.matched)

    if (to.fullPath.startsWith('/app')) {
        return true
    }


    to.meta.matched = [...to.matched]
    const routerStore = useHallRouterStore()
    if (routerStore.routes.length == 0) {
        const hallPages = [
            {
                path: 'dashboard',
                name: 'Dashboard',
                component: 'views/dashboard/index.vue',
                meta: {
                    title: '概览',
                    icon: 'odometer',
                    keepAlive: true,
                },
            },
            {
                path: '404',
                name: '404',
                hidden: true,
                meta: {
                    title: '迷路了*。*',
                },
                component: 'views/error/error.vue'
            },
            {
                path: 'camp',
                name: 'Camp',
                meta: {
                    title: '营地信息管理',
                    icon: 'odometer',
                },
                children: [
                    {
                        path: 'campManager',
                        name: 'CampManager',
                        component: 'views/camp/campManager/index.vue',
                        meta: {
                            title: '营地管理',
                            icon: 'odometer',
                            keepAlive: true,
                        },
                    },
                    {
                        path: 'conditionManager',
                        name: 'ConditionManager',
                        component: 'views/camp/conditionManager/index.vue',
                        meta: {
                            title: '条件管理',
                            icon: 'odometer',
                            keepAlive: true,
                        },
                    },
                ],
            },
            {
                path: 'post',
                name: 'Post',
                meta: {
                    title: '帖子信息管理',
                    icon: 'odometer',
                },
                children: [
                    {
                        path: 'postManager',
                        name: 'PostManager',
                        component: 'views/post/postManager/index.vue',
                        meta: {
                            title: '帖子管理',
                            icon: 'odometer',
                            keepAlive: true,
                        },
                    },
                    {
                        path: 'subjectManager',
                        name: 'SubjectManager',
                        component: 'views/post/subjectManager/index.vue',
                        meta: {
                            title: '专题管理',
                            icon: 'odometer',
                            keepAlive: true,
                        },
                    },
                    {
                        path: 'bannerManager',
                        name: 'BannerManager',
                        component: 'views/post/bannerManager/index.vue',
                        meta: {
                            title: 'Banner管理',
                            icon: 'odometer',
                            keepAlive: true,
                        },
                    },
                ],
            },
            {
                path: 'activity',
                name: 'Activity',
                meta: {
                    title: '活动信息管理',
                    icon: 'odometer',
                },
                children: [
                    {
                        path: 'activityManager',
                        name: 'ActivityManager',
                        component: 'views/activity/activityManager/index.vue',
                        meta: {
                            title: '活动管理',
                            icon: 'odometer',
                            keepAlive: true,
                        },
                    },
                ],
            },
            {
                path: 'sms',
                name: 'Sms',
                meta: {
                    title: '短信管理',
                    icon: 'odometer',
                },
                children: [
                    {
                        path: 'loginWhiteManager',
                        name: 'LoginWhiteManager',
                        component: 'views/sms/loginWhiteManager/index.vue',
                        meta: {
                            title: '登录验证码白名单管理',
                            icon: 'odometer',
                            keepAlive: true,
                        },
                    },
                ],
            },
            {
                path: 'user',
                name: 'User',
                meta: {
                    title: '用户管理',
                    icon: 'odometer',
                },
                children: [
                    {
                        path: 'userManager',
                        name: 'UserManager',
                        component: 'views/user/userManager/index.vue',
                        meta: {
                            title: '用户信息管理',
                            icon: 'odometer',
                            keepAlive: true,
                        },
                    },
                ],
            },
        ]
        const hallRoutes = [
            {
                path: '/hall',
                name: 'Hall',
                component: 'views/hall/index.vue',
                meta: {
                    title: 'hall',
                },
                children: hallPages,
            },
            {
                path: '/:catchAll(.*)',
                redirect: '/hall/404'
            }
        ]
        transformRouterHandle(hallRoutes)
        
        await routerStore.initHallRouter(hallRoutes)
        routerStore.routes.forEach(route => {
            console.log("initHallRouter addroute", route)
            appRouterManger.router.addRoute(route)
        })
        console.log("all routes :", appRouterManger.router.getRoutes())
        return { ...to, replace: true }
    }

    const userStore = useUserStore()
    const uid = userStore.uid
    const sid = userStore.sid
    if (uid > 0 && sid.length > 0) { //已登录
        if (loginPath == to.name) {
            return { name: routerStore.defaultRouter } 
        }
        return true
    } else {
        if (notNeedLoginPaths.indexOf(to.name) > -1) {
            return true
        } else {
            return {
                name: 'Login',
                query: {
                  redirect: document.location.hash
                }
              } 
        }
    }

})

export default appRouterManger